package com.axdoc.baseinfo.dao.impl;

import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;

import com.axdoc.baseinfo.dao.IHospitalMongoDao;
import com.axdoc.baseinfo.entity.Hospital;
import com.axdoc.common.core.mongo.AbstractMongoDao;
import com.axdoc.common.core.pagination.PageInfo;
import com.axdoc.common.core.pagination.PageResults;
import com.mongodb.BasicDBObject;
@Component("hospitalMongoDao")
public class HospitalMongoDaoImpl extends AbstractMongoDao<Hospital> implements IHospitalMongoDao{

	public Hospital getHospital(ObjectId hospitalId) {
		return super.get(Hospital.MONGO_COLLECT_NAME, hospitalId);
	}

	public PageResults<Hospital> findHospitalPage(Hospital hospitalCon, List<String> cityList, PageInfo pageInfo) {
		BasicDBObject query = new BasicDBObject();
		if(!StringUtils.isEmpty(hospitalCon.getName())){
			Pattern namePattern = Pattern.compile("^.*" + hospitalCon.getName() + ".*$", Pattern.CASE_INSENSITIVE);
			query.put("name", namePattern);
		}
		if(!StringUtils.isEmpty(hospitalCon.getCity())){
			query.put("city", hospitalCon.getCity());
		}
		if(!StringUtils.isEmpty(hospitalCon.getLevel())){
			Pattern namePattern = Pattern.compile("^.*" + hospitalCon.getLevel() + ".*$", Pattern.CASE_INSENSITIVE);
			query.put("level", namePattern);
		}
		if(hospitalCon.getCategory() != null){
			query.put("category", hospitalCon.getCategory());
		}
		if(hospitalCon.getMajor() != null){
			query.put("major", hospitalCon.getMajor());
		}
		if(cityList != null){
			query.put("city", new BasicDBObject("$in", cityList.toArray()));
		}
		BasicDBObject orderBy = new BasicDBObject();
		List<Hospital> result = super.find(Hospital.MONGO_COLLECT_NAME, query, orderBy, pageInfo);
		return new PageResults<Hospital>(pageInfo, result);
	}

	public Hospital getHospitalByName(String name) {
		return super.getByPropery(Hospital.MONGO_COLLECT_NAME, "name", name);
	}
	
	public List<Hospital> findHospitalListByName(String name) {
		BasicDBObject query = new BasicDBObject();
		Pattern namePattern = Pattern.compile("^.*" + name + ".*$", Pattern.CASE_INSENSITIVE);
		query.put("name", namePattern);
		return super.find(Hospital.MONGO_COLLECT_NAME, query);
	}

}
